package br.ufal.tci.nexos.arcolive.util;

import br.ufal.tci.nexos.arcolive.persistence.GenericMetaDataColumn;
import br.ufal.tci.nexos.arcolive.persistence.GenericResultRow;
import br.ufal.tci.nexos.arcolive.persistence.GenericResultSet;

public class PermissionManipulator {

	public static GenericResultSet mergePermissions(String field, GenericResultSet permissionSet1, GenericResultSet permissionSet2) {
		GenericResultSet result = permissionSet1;
		String fieldValue1, fieldValue2; 
		boolean found;
		
		if (!permissionSet1.equals(permissionSet2)) {
			return null;
		}
		
		permissionSet1.beforeFirst();
		permissionSet2.beforeFirst();
		
		while (permissionSet2.next()) {
			fieldValue2 = permissionSet2.getValue(field);
			found = false;
			while (permissionSet1.next()) {
				fieldValue1 = permissionSet1.getValue(field);
				if (fieldValue1.equalsIgnoreCase(fieldValue2)) {
					found = true;
					break;
				}
			}
			
			if (!found) {
				result.addRow(permissionSet2.getCurrentRow());
			}
			
		}
		result.beforeFirst();
		return result;
	}
	
	public static void main(String[] args) {
		GenericResultSet r1 = new GenericResultSet(new GenericMetaDataColumn[]{new GenericMetaDataColumn("commandId", "java.lang.Integer"), new GenericMetaDataColumn("name", "java.lang.String")});
		GenericResultSet r2 = new GenericResultSet(new GenericMetaDataColumn[]{new GenericMetaDataColumn("commandId", "java.lang.Integer"), new GenericMetaDataColumn("name", "java.lang.String")});
		
		GenericResultRow row1 = new GenericResultRow();
		row1.put("203");
		row1.put("login");
		
		GenericResultRow row2 = new GenericResultRow();
		row2.put("203");
		row2.put("logout");
		
		r1.addRow(row1);
		r2.addRow(row2);
		
//		Hashtable conditions = new Hashtable();
//		conditions.put("commandId", "203");
//		
//		GenericResultSet searchResult = r1.searchByOneField(conditions);
//		
//		while (searchResult.next()) {
//			ArCoLIVELogRepository.getInstance().log(searchResult.getValue(1));
//		}
			
		
//		GenericResultSet permissionMerged = mergePermissions("commandId", r1, r2);
//		
//		ArCoLIVELogRepository.getInstance().log(permissionMerged + " " + permissionMerged.size());
//		
//		while (permissionMerged.next()) {
//			ArCoLIVELogRepository.getInstance().log(permissionMerged.getValue("name"));
//		}
		
	}
	
}
